<?php
namespace jy_pingpaifee;
defined('BY_JYA') or exit('error');
class team extends \plugin{
	/*
		成团(下单后，跟进订单的teamid)
	*/
	function upteam($order){
		$this->order=$order;
		foreach($order['goods'] as $goods){
			$goods=$this->get('goods',array('goods_id'=>$goods['goodsid'],'uniacid'=>$_SESSION['uniacid']));
			if($goods['id'] && !$goods['ignore'] && $goods['team_rate']){
				$this->order['goodsid']=$goods['goodsid'];
				$pass=true;
			}
		}
		if(!$pass){
			return false;
		}
		if($order['teamid'] && $order['plugin_name']=='jy_pingpaifee'){
			$teamid=$order['teamid'];
		}
		$member=pdo_get('jy_commission_member',array('mid'=>$order['mid']));
		$team=$this->getcanteam($teamid,$member['team_mid']);
		if($team=='isteam'){
			return false;
		}
		$myteam_id=$this->addteam($order['mid'],$team);
		if($team){
			$p_mid=$this->getpaiwei($team);
			$this->addteamlog($team['id'],$order,$p_mid);
			$this->check_upboss($team);
		}else{
			$this->addteamlog($myteam_id);
		}
		return true;
	}
	function addteam($mid=0,$team=array(),$bosslevel=0){
		$mid=$mid?:$_SESSION['uid'];
		$bosslevel=$bosslevel?:$team['bosslevel'];
		$myteam=pdo_get('jy_pingpaifee_team',array('status'=>0,'bosslevel'=>$bosslevel,'mid'=>$mid,'uniacid'=>$_SESSION['uniacid']));
		if($myteam['id']){
			return $myteam['id'];
		}
		$level=intval($this->setting['pai_level']);
		$num=intval($this->setting['pai_num']);
		$level=$level?:1;
		$num=$num?:3;
		$insertdata=array(
				'uniacid'=>$_SESSION['uniacid'],
				'code'=>'SN'.get_ordersn($this->tablename('team'),'code'),
				'mid'=>$mid,
				'level'=>$level,
				'bosslevel'=>$bosslevel,
				'num'=>$num,
				'goodsid'=>$this->order['goodsid'],
				'order_price'=>$this->order['price'],
				'orderid'=>$this->order['id'],
				'status'=>0,
				'createtime'=>TIMESTAMP
		);
		if($team['bossmid']){
			$insertdata['bossmid']=$team['bossmid'];
		}elseif($team['mid']){
			$insertdata['bossmid']=$team['mid'];
		}
		pdo_insert('jy_pingpaifee_team',$insertdata);
		$myteam_id=pdo_insertid();
		return $myteam_id;
	}
	function addteamlog($team_id=0,$order=array(),$p_mid=0){
		$mid=$order['mid']?:$_SESSION['uid'];
		$insertdata=array(
				'uniacid'=>$_SESSION['uniacid'],
				'mid'=>$mid,
				'p_mid'=>$p_mid,
				'team_id'=>$team_id,
				'orderid'=>$order['id'],
				'createtime'=>TIMESTAMP
		);
		pdo_insert('jy_pingpaifee_team_log',$insertdata);
		return true;
	}
	/*
		获取team人头
	*/
	public function getteamnum($team){
		$level=intval($this->setting['pai_level']);
		$num=intval($this->setting['pai_num']);
		$level=$level?:1;
		$num=$num?:3;
		$teamnum=pow($num,$level);
		if($level>1){
			$teamnum=$teamnum+$num;
		}
		return $teamnum;
	}
	/*
		获取team
	*/
	public function checkteam($teamid){
		$ispai=pdo_getcolumn('jy_pingpaifee_team',array('status'=>0,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),'id');
		if($ispai){
			exi('您还有排位在进行中！不能参与其他排位！','error');
		}
		$isnum=pdo_count('jy_pingpaifee_team_log',array('uniacid'=>$_SESSION['uniacid'],'team_id'=>$teamid));
		$team=pdo_get('jy_pingpaifee_team',array('id'=>$teamid,'uniacid'=>$_SESSION['uniacid']));
		$teamnum=$this->getteamnum($team);
		if($isnum>0 && $isnum==$teamnum){
			exi('该排位已满！','error');
		}
		return true;
	}
	/*
		升级树
	*/
	public function check_upboss($team=array()){
		$team_mid=pdo_get('jy_commission_member',array('mid'=>$team['mid']),'team_mid');
		$isnum=pdo_count('jy_pingpaifee_team_log',array('uniacid'=>$_SESSION['uniacid'],'team_id'=>$team['id']));
		$teamnum=$this->getteamnum($team);
		if($isnum>0 && $isnum==$teamnum){
			pdo_update('jy_pingpaifee_team',array('status'=>1),array('uniacid'=>$_SESSION['uniacid'],'id'=>$team['id']));
			$this->upteam_fee($team);
			$nextbosslevel=$this->get_nextbosslevel($team);
			$pteam=$this->getcanteam(0,$team_mid,$nextbosslevel,$team['mid']);
			if($pteam=='isteam'){
				return false;
			}
			$nextbosslevel=$nextbosslevel==-1?0:$nextbosslevel;
			$myteam_id=$this->addteam($team['mid'],$pteam,$nextbosslevel);
			if($pteam){
				$p_mid=$this->getpaiwei($pteam);
			}
			if($pteam['id']){
				$this->addteamlog($pteam['id'],$this->order,$p_mid);
			}else{
				$this->addteamlog($myteam_id,$this->order,$p_mid);
			}
		}
		return true;
	}
	/*
		获取team
	*/
	public function getcanteam($teamid=0,$team_mid=0,$bosslevel=0,$mid=0){
		$mid=$mid?:$_SESSION['uid'];
		$myteam=pdo_get('jy_pingpaifee_team',array('mid'=>$mid,'status'=>0,'uniacid'=>$_SESSION['uniacid']));
		if($myteam['id']){
			return 'isteam';
		}
		if($teamid){
			$team=pdo_get('jy_pingpaifee_team',array('status'=>0,'id'=>$teamid,'uniacid'=>$_SESSION['uniacid']));
		}
		if(!$team && $bosslevel){
			$bosslevel=$bosslevel==-1?0:$bosslevel;
			$team=pdo_get('jy_pingpaifee_team',array('bosslevel'=>$bosslevel,'status'=>0,'uniacid'=>$_SESSION['uniacid']));
		}
		if(!$team && $team_mid && !$bosslevel){
			$team=pdo_get('jy_pingpaifee_team',array('status'=>0,'mid'=>$team_mid,'uniacid'=>$_SESSION['uniacid']));
		}
		if(!$team && !$bosslevel){
			$team=pdo_get('jy_pingpaifee_team',array('bosslevel'=>$myteam['bosslevel'],'status'=>0,'uniacid'=>$_SESSION['uniacid']));
		}
		return $team;
	}
	/*
		获取排位
	*/
	public function getpaiwei($team){
		$level=intval($this->setting['pai_level']);
		$num=intval($this->setting['pai_num']);
		$level=$level?:2;
		$num=$num?:3;
		$p_mid=$team['mid'];
		$isnum=pdo_count('jy_pingpaifee_team_log',array('p_mid'=>$p_mid,'team_id'=>$team['id']));
		if($isnum<$num){
			return $p_mid;
		}
		if($level>=2){
			$p_mids=$p_mid;
			for($i=2;$i<=$level;$i++){
				$p_mids=pdo_getall('jy_pingpaifee_team_log',array('p_mid'=>$p_mids,'team_id'=>$team['id']),'mid','mid');
				if($p_mids){
					$p_mids=array_keys($p_mids);
					foreach($p_mids as $p_mid){
						$isnum=pdo_count('jy_pingpaifee_team_log',array('p_mid'=>$p_mid,'team_id'=>$team['id']));
						if($isnum<$num){
							return $p_mid;
						}
					}
				}else{
					return $p_mid;
				}
			}
		}
		return false;
	}
	/*
		获取排位排列
	*/
	public function getpaiweilogs($team){
		$level=intval($this->setting['pai_level']);
		$num=intval($this->setting['pai_num']);
		$level=$level?:2;
		$num=$num?:3;
		$paiweilogs=array();
		$p_mids=$team['mid'];
		for($i=1;$i<=$level;$i++){
				$paiweilogs[$i]=array();
				$logs=pdo_getall('jy_pingpaifee_team_log',array('p_mid'=>$p_mids,'team_id'=>$team['id']),'','mid');
				if($logs){
					foreach($logs as &$item){
						$item['member']=cfc('core')->member_show($item['mid']);
						$item['member']['avatar']=$item['member']['avatar']?tomedia($item['member']['avatar']):'';
						if($item['mid']==$team['mid']){
							$nickname=$item['member']['nickname'];
						}
						$item['createtime']=date('Y-m-d h:i',$item['createtime']);
					}
					$p_mids=array_keys($logs);
				}
				if($i==1){
					$levelnum=$num;
				}else{
					$levelnum=pow($num,$level);
				}
				$lastn=$levelnum-count($logs);
				$lastlogs=array();
				for ($k=1; $k<=$lastn; $k++) {
					$lastlogs[]=array('no'=>$k);
				}
				$paiweilogs[$i]=array(
					'logs'=>$logs,
					'lastlogs'=>$lastlogs,
				);
		}
		return $paiweilogs;
	}
	/*
		成团奖励
	*/
	function upteam_fee($team){
		$goods=$this->get('goods',array('goods_id'=>$team['goodsid'],'uniacid'=>$_SESSION['uniacid']));
		if(!$goods['id'] || $goods['ignore'] || $goods['team_rate']){
			return false;
		}
		if(substr($goods['team_rate'],-1)=='%'){
				$money=intval($team['order_price']*substr($goods['team_rate'],0,-1))/100;
		}else{
				$money=$goods['team_rate'];
		}
		$pai_tuifee=intval($this->setting['pai_tuifee']);
		$money=$pai_tuifee*$order['price']/100;
		$new_commission=array(
					'uniacid'=>$_SESSION['uniacid'],
					'mid'=>$team['mid'],
					'orderid'=>$team['orderid'],
					'money'=>$money,
					'createtime'=>TIMESTAMP,
					'status'=>0,
					'type'=>6,
					'level'=>1,
		);
		pdo_insert('jy_newpingfee_commission',$new_commission);
		return true;
	}
	/*
		排位奖励
	*/
	function uppai_fee($order,$mid){
		$pai_fee=intval($this->setting['pai_fee']);
		$money=$pai_fee*$order['price']/100;
		$new_commission=array(
					'uniacid'=>$_SESSION['uniacid'],
					'mid'=>$mid,
					'level'=>2,
					'ordersn'=>$order['ordersn'],
					'money'=>$money,
					'pluginname'=>'jy_pingpaifee',
					'createtime'=>TIMESTAMP,
					'status'=>0,
		);
		pdo_insert('jy_commission_commission',$new_commission);
		return true;
	}
	/*
		获取当前轮次
	*/
	function getlevel($mid=0){
		$mid=$mid?:$_SESSION['uid'];
		$bosslevel=pdo_getcolumn('jy_pingpaifee_team',array('status'=>0,'mid'=>$mid,'uniacid'=>$_SESSION['uniacid']),'bosslevel');
		return $bosslevel;
	}
	/*
		获取
	*/
	function get_nextbosslevel($team=array()){
		$pai_jieti=$this->setting['pai_jieti'];
		$bosslevel=$team['bosslevel'];
		paixu($pai_jieti,'level','desc');
		foreach($pai_jieti as $k=>$pv){
			if($bosslevel>=$pv['level']-1){
				$startlevel=$pv['level']-1;
				if($k){
					$nextlevel=$pai_jieti[$k-1]['level']-1;
				}else{
					$ispai_maxnum=1;
					//最大轮不减去1
					$nextlevel=$this->setting['pai_maxnum'];
				}
				$levelnum=$nextlevel-$startlevel;
				$isnum=pdo_count('jy_pingpaifee_team',array('bosslevel between'=>array($startlevel,$nextlevel-1),'status'=>1,'mid'=>$mid,'uniacid'=>$_SESSION['uniacid']),'id');
				if($ispai_maxnum && $bosslevel==$nextlevel-1 && $isnum>=$levelnum){
					$nextbosslevel=-1;
					break;
				}
				if($bosslevel==$nextlevel-1 && $isnum>=$levelnum){
					$nextbosslevel=$nextlevel;
					break;
				}
				if($bosslevel<$nextlevel-1 && $isnum>=$levelnum){
					$nextbosslevel=$bosslevel+1;
					break;
				}
				if($bosslevel==$nextlevel-1 && $isnum<$levelnum){
					$nextbosslevel=$startlevel;
					break;
				}
			}
		}
		if($nextbosslevel>=$this->setting['pai_maxnum']-1){
			$nextbosslevel=-1;
		}
		return $nextbosslevel?:$bosslevel+1;
	}
	/*
		获取挡位商品数量
	*/
	function get_dangnum($mid=0){
			$pai_jieti=$this->setting['pai_jieti'];
			$level=$this->getlevel($mid);
			$level=$level+1;
			paixu($pai_jieti,'level','desc');
			foreach($pai_jieti as $pv){
				if($level>=$pv['level']){
					return $pv['num'];
				}
			}
			return 1;
	}
	/*
		确定轮次商品数量
	*/
	function check_num($goods_id=0,$num=1){
		if($goods_id==$this->setting['goods_id']){
			$pai_jieti=$this->setting['pai_jieti'];
			$mid=$_SESSION['uid'];
			$member=pdo_get('jy_commission_member',array('mid'=>$_SESSION['uid']));
			if($member['agent_id']){
				$team=pdo_get('jy_pingpaifee_team',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
				if(!$team['id']){
					$mid=$member['agent_id'];
				}
			}
			$level=$this->getlevel($mid);
			$level=$level+1;
			paixu($pai_jieti,'level','desc');
			foreach($pai_jieti as $pv){
				if($level>=$pv['level'] && $num<$pv['num']){
					exi('您当前处于'.$level.'轮！必须购买'.$pv['num'].'件商品，才能参与！','error');
				}
			}
		}
		return true;
	}
}